﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhsystem.Main;
using dhSystemBLL.produceBLL;
using baseSet.importExport;
using dhDataAcess.dataAcess;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using FastReport;
using baseSet.power;

namespace dhsystem.produceForm
{

    public partial class FrmcgddMaster : Form
    {
        cgddBLL cgdd = new cgddBLL();
        string state;
        public FrmcgddMaster()
        {
            InitializeComponent();
        }
        //打开窗口时显示列表数据
        private void FrmcgddMaster_Load(object sender, EventArgs e)
        {

            getData();

            //bindingSourceMaster.DataSource = cgdd.getcgddMasterAll();
            //dGridMaster.DataSource = bindingSourceMaster;
            //DcolumnSet.colSet(moduleInfo.cgddMaster, dGridMaster);
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
            state = "新增";
            FrmcgddMasterAdd cgddMasterAdd = new FrmcgddMasterAdd(bindingSourceMaster, state);
            cgddMasterAdd.ShowDialog();
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.rltkMaster);
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridDetail.DataSource = query.rTable;
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            state = "编辑";

            FrmcgddMasterAdd cgddMasterAdd = new FrmcgddMasterAdd(bindingSourceMaster, state);
            cgddMasterAdd.ShowDialog();
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string billNo = dGridMaster.Rows[dGridMaster.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除：" + billNo + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceMaster.DataSource = cgdd.cgddMasterDel(billNo);

                }
                getData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {

        }
        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            getData();
        }
        //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //datagridview第一列显示行号
        private void dGridMaster_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
        //datagridview第一列显示行号
        private void dGridDetail_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
        private void getData()
        {
            cgddDetailTableAdapter  cgddDetailAdapter = new cgddDetailTableAdapter();
            cgddMasterTableAdapter cgddMasterAdapter = new cgddMasterTableAdapter();
            purchase cgdd = new purchase();
            cgddMasterAdapter.FillcgddMasterALL(cgdd.cgddMaster);
            cgddDetailAdapter.FillcgddDetailAll (cgdd .cgddDetail );
          
            bindingSourceMaster.DataSource = cgdd;
            bindingSourceMaster.DataMember = "cgddMaster";
            dGridMaster.DataSource = bindingSourceMaster;

            DcolumnSet.colSet(moduleInfo.cgddMaster, dGridMaster);
            toolStripStatusLabel1.Text = "领料主表数据总条数:" + dGridMaster.RowCount.ToString() + "条";
            bindingSourceDetail.DataSource = bindingSourceMaster;

            bindingSourceDetail.DataMember = "fk_cgddDetail_billNo";
            dGridDetail.DataSource = bindingSourceDetail;
            DcolumnSet.colSet(moduleInfo.cgddDetail, dGridDetail);
            toolStripStatusLabel3.Text = "领料从表数据总条数:" + dGridDetail.RowCount.ToString() + "条";

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.cgddMaster );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.cgddMaster , dGridMaster , "采购订单信息");
            frm.ShowDialog();
        }

        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridMaster.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(cgdd.getV_cgddbaobiaoAllBybillNo(billNo));
            // r.Load(@"E:\东华软件项目\报表\cgddbaobiao.frx");
            r.Load(@"\\192.168.0.27\东华软件\报表\cgddbaobiao.frx");
            r.RegisterData(ds);
            //r.Design();
            r.Show();
        }
    }
}
